package com.hxrfid.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxrfid.common.core.domain.PageQuery;
import com.hxrfid.common.core.page.TableDataInfo;
import com.hxrfid.common.helper.LoginHelper;
import com.hxrfid.common.utils.StringUtils;
import com.hxrfid.project.domain.BsMoneyRecord;
import com.hxrfid.project.domain.bo.BsMoneyRecordBo;
import com.hxrfid.project.domain.vo.BsMoneyRecordVo;
import com.hxrfid.project.mapper.BsMoneyRecordMapper;
import com.hxrfid.project.service.IBsMoneyRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 余额变动记录Service业务层处理
 *
 * @author hxrfid
 * @date 2025-06-12
 */
@RequiredArgsConstructor
@Service
public class BsMoneyRecordServiceImpl extends ServiceImpl<BsMoneyRecordMapper, BsMoneyRecord> implements IBsMoneyRecordService {

    private final BsMoneyRecordMapper baseMapper;

    /**
     * 查询余额变动记录
     * @param id 主键ID
     * @return 余额变动记录
     */
    @Override
    public BsMoneyRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询余额变动记录列表
     * @param bo 余额变动记录对象
     * @param pageQuery 主键ID
     * @return 余额变动记录列表
     */
    @Override
    public TableDataInfo<BsMoneyRecordVo> queryPageList(BsMoneyRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BsMoneyRecord> lqw = buildQueryWrapper(bo);
        Page<BsMoneyRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询余额变动记录列表
     * @param bo 余额变动记录对象
     * @return 余额变动记录列表
     */
    @Override
    public List<BsMoneyRecordVo> queryList(BsMoneyRecordBo bo) {
        LambdaQueryWrapper<BsMoneyRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
    * 组装查询条件
    * @param bo 余额变动记录对象
    * @return 查询条件
    */
    private LambdaQueryWrapper<BsMoneyRecord> buildQueryWrapper(BsMoneyRecordBo bo) {
        if (!LoginHelper.isAdmin() && !LoginHelper.isTenantAdmin()) {
            bo.setUserId(LoginHelper.getUserId());
        }
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsMoneyRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), BsMoneyRecord::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getChangeNum()), BsMoneyRecord::getChangeNum, bo.getChangeNum());
        lqw.eq(StringUtils.isNotBlank(bo.getChangeType()), BsMoneyRecord::getChangeType, bo.getChangeType());
        lqw.eq(bo.getUserId() != null, BsMoneyRecord::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getPayType()), BsMoneyRecord::getPayType, bo.getPayType());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), BsMoneyRecord::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getOrderTime() != null, BsMoneyRecord::getOrderTime, bo.getOrderTime());
        lqw.eq(bo.getRealPrice() != null, BsMoneyRecord::getRealPrice, bo.getRealPrice());
        lqw.orderByDesc(BsMoneyRecord::getCreateTime);
        return lqw;
    }

    /**
     * 新增余额变动记录
     * @param bo 余额变动记录对象
     * @return 是否成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean insertByBo(BsMoneyRecordBo bo) {
        BsMoneyRecord add = BeanUtil.toBean(bo, BsMoneyRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 批量新增余额变动记录
     * @param boList 批量余额变动记录对象
     * @return 是否成功
     */
    @Override
    public Boolean batchInsertByBo(List<BsMoneyRecordBo> boList) {
        List<BsMoneyRecord> addList = BeanUtil.copyToList(boList, BsMoneyRecord.class);
        return baseMapper.insertBatch(addList);
    }

    /**
     * 修改余额变动记录
     * @param bo 余额变动记录对象
     * @return 是否成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean updateByBo(BsMoneyRecordBo bo) {
        BsMoneyRecord update = BeanUtil.toBean(bo, BsMoneyRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     * @param entity 余额变动记录对象
     */
    private void validEntityBeforeSave(BsMoneyRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除余额变动记录
     * @param ids 主键ID的list
     * @param isValid 是否业务校验
     * @return 是否成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
